<?php
	require_once("includes/db_connection.php");
	require_once("includes/functions.php");
	include("includes/main-header.php");
?>

<table id="structure">
	<tr>
		<td id="navigation">
			<h2>Menu:</h2>
			<ul>
				<li><a href="/">strona glowna</a></li>
			</ul>
		</td>
		<td id="page">
			
			<h2>Logowanie</h2>
			
			<?
			function login_form() {
			global $PHP_SELF;
		
			?>
			
			<form method="post" action="<?php echo $PHP_SELF ?>">
				<input type="hidden" name="action" value="login">
				<p>Nazwa uzytkownika:</p>
				<p><input type="text" name="username" value="" size="15" maxlenght="15"></p>
				<p>Haslo:</p>
				<p><input type="password" name="password" value="" size="15" maxlenght="32"></p>
				<input type="submit" name="login" value="Loguj">
			</form>
			
			<?
			
			}
			
			function login() {
			
				global $PHP_SELF;
				global $username, $password;
					$password = md5($password);
				
				$query = "SELECT username, password, id, superuser FROM users WHERE username = '$username'";
				$result = mysql_query($query);
				
				while($query_data = mysql_fetch_array($result)) {
					$password2 = $query_data["password"];
					
					if($password == $password2) {
						echo "<h3>Zalogowano! jako $username.</h3>";
						echo "Przejdz do <a href=\"panel.php\">panelu</a>!";
						
						setcookie('user_id', $query_data["id"], time() + 30 * 60);
						setcookie('superuser', $query_data["superuser"], time() + 30 * 60);

					} else	{
						echo "Bledny login lub/i haslo. Zaloguj sie <a href=\"login.php\">ponownie</a>";
					}
				
				}
			}

			switch($action) {
				case "login":
					login();
				break;
				default:
					login_form();
				break;
			}
			?>
			
		</td>
	</tr>
</table>
<?php require("includes/footer.php"); ?>